home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7068 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  73 lines

  1. Path: news.acns.nwu.edu!aragorn232
  2. From: llong@nwu.edu (Lorraine E. Long)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help for a float problem
  5. Date: Sun, 18 Feb 96 03:57:39 GMT
  6. Organization: Northwestern University, Evanston, IL.   USA
  7. Message-ID: <4g683j$99g_001@nuts.nwu.edu>
  8. References: <AMCHAVAN.96Jan25112007@ac2.hq.eso.org> <4f6tt6$rt1@news.cencom.net> <311A83D1.57A3@feith.com> <4fe4m4$f1c@news.ee.net> <4fgbss$nqm@ixnews6.ix.netcom.com> <9FEB199622285820@erich.triumf.ca>
  9. NNTP-Posting-Host: aragorn232.nuts.nwu.edu
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <9FEB199622285820@erich.triumf.ca>,
  13.    bennett@erich.triumf.ca (P.Bennett) wrote:
  14. >In article <4fgbss$nqm@ixnews6.ix.netcom.com>, wzjn@ix.netcom.com (KPN ) 
  15. writes...
  16. >>Good Afternoon,
  17. >> 
  18. >>Using æHow To Program C (second edition)Æ by Deitel & Deitel, IÆm
  19. >>trying to do on page 92, example 3.17. IÆm not sure, but believe that
  20. >>my problem is where to use a æfloatÆ. 
  21. >> 
  22. >>I want to ask the user to enter gallons used, which of course, could be
  23. >>12.8. However, with the same line, I want the user to enter æ-1Æ if
  24. >>they choose to end the program.
  25. >> 
  26. >>The way it is written so far, the user can successfully exit the
  27. >>program with a -1. The user can also successfully enter whole numbers
  28. >>and get an answer. My problem arises when I enter (again) 12.8 ... the
  29. >>program goes in to an infinate loop! eh?
  30. >> 
  31. >>Conversely, if I define gallons as a float, the program does not
  32. >>recognize the æ-1Æ as an end of program. eh? 
  33. >> 
  34. >
  35. >If you expect the user to enter a decimal value like 12.8 for gallons (or
  36. >miles), you must declare gallons as float.
  37. >
  38. >However, when the user enters -1 to quit, you will be caught by the fact that
  39. >floating point numbers are never exact (Although I would expect -1 to be), so
  40. >it is unwise to compare two floats for equality.
  41. >
  42. >You could compare their difference to some small number, or, for this 
  43. problem,
  44. >simply consider any negative number as the signal to quit.
  45. >
  46. >
  47. >Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  48. >Internet: bennett@triumf.ca         | of one another only when one can be
  49. >Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  50. >TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  51. >GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  52. >
  53. >Would it work to use:
  54.  
  55.     while -1 >= gallons{
  56.         statement;
  57.     }
  58. or
  59.     if -1 >= gallons
  60.         statement;
  61. >
  62. >That would cover any difference at some remote degree of accuracy.
  63. >
  64. >
  65. >
  66. >
  67. >
  68.  
  69.  
  70. Lorraine E. Long
  71. Northwestern University, Evanston, IL.   USA
  72. llong@nwu.edu
  73.